<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.bannerName"
        clearable
        style="width: 200px;"
        placeholder="请输入轮播名称"
      />
      <el-button type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button type="primary" icon="el-icon-edit" @click="addCourse">添加轮播</el-button>
      <el-button
        :disabled="multipleSelection.length===0"
        type="danger"
        icon="el-icon-delete"
        @click="handleDeleteAll"
      >批量删除</el-button>
    </div>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="首页轮播" name="first" />
      <el-tab-pane label="课程视频轮播" name="second" />
    </el-tabs>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column align="center" width="100px" label="ID" prop="bannerId" />
      <el-table-column align="center" label="名称" prop="bannerName" />
      <el-table-column align="center" label="跳转类型" prop="forwardType">
        <template
          slot-scope="scope"
        >{{ scope.row.forwardType==0?'商品':scope.row.forwardType==2?'链接':'课程视频' }}</template>
      </el-table-column>
      <el-table-column align="center" label="图片" prop="mobile">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.bannerImage"
            :preview-src-list="[scope.row.bannerImage]"
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="排序" prop="sorting" />
      <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <el-badge
            :type="scope.row.status == 0?'danger':'success'"
            is-dot
            class="item"
          >{{ scope.row.status == 0 ? '隐藏':'显示' }}</el-badge>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            v-permission="['POST /admin/category/update']"
            type="primary"
            size="mini"
            @click="onEdit(scope.row)"
          >编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row.bannerId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.size"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { bannerList, bannerDel } from '@/api/banner'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'User',
  components: { Pagination },
  data() {
    return {
      activeName: 'first',
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        index: 0,
        size: 10,
        bannerType: 'first'
      },
      multipleSelection: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(e) {
      const activeName = this.activeName
      if (e && e.page) {
        this.listQuery.index = e.page - 1
        this.listQuery.bannerType = activeName
      }
      this.listLoading = true
      bannerList(this.listQuery)
        .then(response => {
          this.list = response.data.data.content
          this.total = response.data.data.totalRecords
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.index = 0
      this.listQuery.page = 1
      this.listQuery.bannerType = this.activeName
      this.getList()
    },
    handleClick(tab) {
      this.activeName = tab.name
      this.handleFilter()
    },
    addCourse() {
      this.$router.push({ name: 'bannerAdd' })
    },
    handleDeleteAll() {
      const idList = []
      this.multipleSelection.forEach(item => {
        idList.push(item.bannerId)
      })
      bannerDel({ idList }).then(res => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const params = {
            idList: [id]
          }
          bannerDel(params).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getList()
          })
        })
        .catch(e => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    onEdit(item) {
      this.$router.push({
        path: '/operation/bannerEdit',
        query: { data: JSON.stringify(item) }
      })
    }
  }
}
</script>
